package com.iwdnb.gkgz.common.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

import com.iwdnb.gkgz.common.model.dto.StockDayData;

/**
 * 股票均价计算
 */
public class StockMaLineUtils {

    /**
     * 计算股票N日均价
     *
     * @param data
     * @param dayCount
     * @return
     */
    public static List<BigDecimal> calculateMA(List<StockDayData> data, int dayCount) {
        List<BigDecimal> result = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            if (i < dayCount) {
                result.add(null);
                continue;
            }
            BigDecimal sum = BigDecimal.ZERO;
            for (int j = 0; j < dayCount; j++) {
                sum = sum.add(data.get(j).getClosePrice());
            }
            BigDecimal maPrice = sum.divide(new BigDecimal(dayCount), BigDecimal.ROUND_HALF_DOWN).setScale(2,
                RoundingMode.HALF_DOWN);
            result.add(maPrice);
        } return result;
    }
}
